package cn.school.mybatis.service;

import cn.school.mybatis.Util.MyBatisUtil;
import cn.school.mybatis.dao.ResultDao;
import cn.school.mybatis.pojo.Result;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//业务逻辑bean
//服务层,调用dao方法
public class ResultService {
    public final static ResultService RESULT_SERVICE = new ResultService();
    // 查询成绩的同时获得科目和学生信息
    public Result get(int id) {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        ResultDao resultdao = session.getMapper(ResultDao.class);
        return resultdao.get(id);
    } finally {
        session.commit();
        MyBatisUtil.close(session);
    }
}
//增加
public int add(Result result) {
    SqlSession session = MyBatisUtil.getSqlSession();
    try {
        ResultDao resultdao = session.getMapper(ResultDao.class);
        return resultdao.add(result);
    } finally {
        session.commit();
        MyBatisUtil.close(session);
    }
}
    //模糊分页查询
    public List<Result> search(int pageNo, int pageSize, Map<String, Object> condition) {
        SqlSession session = MyBatisUtil.getSqlSession();
        ResultDao resultDao;
        int offset = (pageNo - 1) * pageSize;
        offset = Math.max(offset, 0);
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", offset);
        map.put("pageSize", pageSize);
        map.put("orderBy", "id asc");
        if (condition.size() > 0) {
            for (String key : condition.keySet()) {
                map.put(key, condition.get(key));
            }
        }
        try {
            resultDao = session.getMapper(ResultDao.class);
            List<Result> list = resultDao.search(map);
            return list;
        } finally {
            MyBatisUtil.close(session);
        }
    }
    //模糊查数量
//    public int findTotal(Map map) {
    public int findTotal() {
        SqlSession session = MyBatisUtil.getSqlSession();
        try {
            ResultDao resultDao = session.getMapper(ResultDao.class);
            int count = resultDao.searchCount();
            return count;
        } finally {
            MyBatisUtil.close(session);
        }
    }

////删除
//public int del(int id) {
//    SqlSession session = MyBatisUtil.getSqlSession();
//    try {
//        AdminDao dao = session.getMapper(AdminDao.class);
//        return dao.del(id);
//    } finally {
//        session.commit();
//        MyBatisUtil.close(session);
//    }
//}
////修改
//public int update(Admin admin) {
//    SqlSession session = MyBatisUtil.getSqlSession();
//    try {
//        AdminDao dao = session.getMapper(AdminDao.class);
//        return dao.update(admin);
//    } finally {
//        session.commit();
//        MyBatisUtil.close(session);
//    }
//}
////id查单个
//public Admin get(int id) {
//    SqlSession session = MyBatisUtil.getSqlSession();
//    try {
//        AdminDao dao = session.getMapper(AdminDao.class);
//        return dao.get(id);
//    } finally {
//        MyBatisUtil.close(session);
//    }
//}
//
//    //账号密码查单个
//    public Admin getUser(String username,String password) {
//        SqlSession session = MyBatisUtil.getSqlSession();
//        Map map=new HashMap();
//        map.put("username",username);
//        map.put("password",password);
//        try {
//            AdminDao dao = session.getMapper(AdminDao.class);
//            return dao.searchUser(map);
//        } finally {
//            MyBatisUtil.close(session);
//        }
//    }





}
